#!/bin/bash

if [ "$#" != "2" -a "$#" != "3" ]; then
    echo "$0 2022-2023学年第2学期_网络工程2017_数据结构_网络211 'D:\zhang\BTSync\GIT\2025评估\23-25学年_试卷-成绩_张森\22-23-2_试卷-成绩_数据结构_张森_大数据213-网络211-网络212\考试数据包'"
    exit 1
fi 

# 配置

export set_score_fields="${SCORE_FIELDS:=1,2,10}"
sort_field=$(cut -d, -f 3 <<< "$set_score_fields")

# 从https://gitee.com/senzhanggit/doc_format_conv 下载或者用git 克隆到本地，
# 把对应的本地目录的路径设置为local_repos_path的值
local_repos_path=${LOCAL_TOOL_PATH:="H:/devworks/win_file_format_conv"}
unix_local_repos_path="$(cygpath -u -a $local_repos_path)"
export PATH=$PATH:$unix_local_repos_path

# 2022-2023学年第1学期_网络空间安全_Linux操作系统高级应用_网安201
archive_info="$1"
exam_papers_src_path="$2"
toc_tmpl_file="${3:=../../目录模板/.00.00汇总三页目录.docx}"
# echo "hihi ${archive_info}"
IFS=_ read -r term major course class <<< "$archive_info"
cd $archive_info
pwd

# if test ! -d "01.学生试卷" -o "$(ls -1 01.学生试卷 |wc -l)" = "0"; then
# fi

if test ! -f "exam_paper_scores.txt"; then
    echo "找不到分数文件 exam_paper_scores.txt. Exit."
    exit 1
fi

echo "复制试卷"
comm -12 <(find "${exam_papers_src_path}" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" |sort) <(cut -f 1 exam_paper_scores.txt |sort ) |xargs -I{} grep {} exam_paper_scores.txt | cut -f "${set_score_fields}" | exam_paper_pick_3each.sh "${exam_papers_src_path}"

# 1.生成目录
# TODO: remove alphabet characters from stu_id
find ./01.学生试卷/ -maxdepth 1 -mindepth 1 -printf "%f\n" |tr -d '[A-Z]' |xargs -I{} grep {} exam_paper_scores.txt |cut -f ${set_score_fields} |sort -r -n -k 3 | awk -f $unix_local_repos_path/organize_exam_papers.awk - |tr -d '[A-Z]'|xargs exam_paper_replace_grade_level_lists.sh $toc_tmpl_file 00.00.toc_gen.docx $archive_info

# find ./01.学生试卷/ -maxdepth 1 -mindepth 1 -printf "%f\n" |tr -d '[A-Z]' |xargs -I{} grep {} exam_paper_scores.txt |cut -f ${set_score_fields} |sort -r -n -k 3 | awk -f $unix_local_repos_path/organize_exam_papers.awk - |tr -d '[A-Z]'|xargs exam_paper_replace_grade_level_lists.sh ../../目录模板/.00.01学生试卷目录.docx 00.01.exam_paper_toc.docx $archive_info
# cp ../../目录模板/02.00材料目录.docx ./

# 2. Excel转pdf
powershell.exe -ExecutionPolicy Bypass -File "${local_repos_path}/ConvertExcelTo-PDF.ps1"

# 3. Word转pdf
powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "Import-Module ${local_repos_path}/ConvertWordTo-PDF.psm1; ConvertWordTo-PDF -SourceFolder ."

# 4. 分裂目录页为三份

qpdf --empty --pages 00.00.toc_gen.pdf 1 -- 00.00.toc_first.pdf
qpdf --empty --pages 00.00.toc_gen.pdf 2 -- 00.01.exam_paper_toc.pdf
qpdf --empty --pages 00.00.toc_gen.pdf 3 -- 02.00材料目录.pdf

rm -f 00.00.toc_gen.pdf
# 9. 整合pdf
output_pdf_filename="${term}_${course}_${class}_张森.pdf"
# qpdf --empty --pages $(find . -name "*.pdf" |tr '\n' ' ') -- "../${output_pdf_filename}"
find . -type f -name '*.pdf' -printf "%p\0" | xargs -0 bash -c 'qpdf --empty --pages "$@" -- ../${output_pdf_filename}'

echo "生成完毕：" $(realpath "../${output_pdf_filename}")

cd -

# exam_paper_replace_grade_level_lists.sh ../../目录模板/.00.00主目录.docx 00.00.toc_gen.docx $(find ./01.学生试卷/ -maxdepth 1 -mindepth 1 -printf "%f\n" |xargs -I{} grep {} exam_paper_scores.txt |sort -r -n -k 12 |cut -f 1,2,12 | awk -f ../../organize_exam_papers.awk -)
